require_relative "../Expr"

# Expression for functions and lambda expression
#
# @author Paul T.
class Calcula::Exprs::FuncExpr < Calcula::Expr

  # @param params [Calcula::Exprs::ParamsExpr] Parameters to the function
  # @param action [Calcula::Expr] The body of the function
  def initialize(params, action)
    @params = params
    @action = action
  end

  # @see Calcula::Expr#to_s
  # @param (see Calcula::Expr#to_s)
  # @return (see Calcula::Expr#to_s)
  def to_s(form: :src)
    paramsTxt = @params.to_s(form: form)
    actionTxt = @action.to_s(form: form)
    case form
    when :src then
      "\\#{paramsTxt}(#{actionTxt})"
    when :tree then
      "(lambda #{paramsTxt} #{actionTxt})"
    when :ruby then
      "->#{paramsTxt}{#{actionTxt}}"
    else
      nil
    end
  end

  # @see Calcula::Expr#children
  # @param (see Calcula::Expr#children)
  # @return (see Calcula::Expr#children)
  def children
    [@params, @action]
  end
end
